# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 03:55:15 2020

@author: HP
"""

import pandas as pd
from os.path import isfile
from permtest_simulate import permtest_simulate

#====================================================
#Output File Management
#====================================================

fileout = 'simulations.xls'
#column names of the result table
column_names = ['cidtest', 'e_sim', 'N', 'L', 
                'sim_mode', 'perm_mode', 'e_score', 'p_value']

#If the outfile does not exist then make it.
if not isfile(fileout):
    df = pd.DataFrame(columns=column_names)
    df.to_excel(fileout)
df_out = pd.read_excel(fileout,index_col=0)

#====================================================
#Variation Management
#====================================================

Nlist = [6,8,10,12,14,16,18,20]
Llist = [2,3,4,5,6,8,16]
e_list = [.99, .95, .9, .8, .7]
sim_modes = ['r','b']
simN = 100 #Number of sims to run for each variant

#====================================================
#RUN SIMULATIONS
#====================================================

for sim_mode in sim_modes:
    for e_sim in e_list:
        for N in Nlist:
            for L in Llist:
                print('working on ' + 'sm' + sim_mode + 'e' + str(int(100*e_sim)) + 
                      'N' + str( N ) + 'L' + str(L) ) 
                
                if any( ( df_out['sim_mode'] == sim_mode ) & 
                        ( df_out['e_sim'] == e_sim ) &
                        ( df_out['N'] == N ) & 
                        ( df_out['L'] == L ) ):
                    continue #skip if we have already done it.                
                
                e_score_list = []
                p_value_list = []
                
                #Run simN 
                for m in range(simN):
                    e_score, p_value = permtest_simulate(N,L,e_sim, sim_mode,'r')
                    e_score_list.append(e_score)
                    p_value_list.append(p_value)
                outrow_list = [ list(range(simN)), e_sim, N, L, sim_mode, 'r' ]
                outrow_list.extend([e_score_list,p_value_list])
                outrow_dict = dict( zip( column_names, outrow_list ) )
                outrow_df = pd.DataFrame( outrow_dict )
                df_out = df_out.append(outrow_df)
                df_out.to_excel(fileout)


#====================================================
#CALCULATE RESULTS
#====================================================
                

#====================================================
#Results File Management
#====================================================

fileresults = 'test_results.xlsx'
#column names of the result table
column_names = ['test','sim_mode','e_sim', 'L', 'N','e_score_avg', \
                'p_value_10', 'p_value_5', 'p_value_1']

test_name = 'garp'
#Dataframe to output results
df_results = pd.DataFrame(columns=column_names)

#====================================================
#Variants
#====================================================

siglvls = [0.1,0.05,0.01]

#====================================================
#Compile Results
#====================================================


for sim_mode in sim_modes:
    for e_sim in e_list:
        for N in Nlist:
            for L in Llist:
                #new row for df_out
                row_list = [ 'garp',sim_mode,e_sim,L,N ]
                #active indices
                actind = \
                    (df_out['sim_mode']==sim_mode) & \
                    (df_out['N'] == N) & \
                    (df_out['L'] == L) & \
                    (df_out['e_sim'] == e_sim)
                #e-score average
                e_score_avg = df_out['e_score'][actind].mean()
                #Add e-score to rowlist
                row_list.append(e_score_avg)
                
                #p-values
                p_col = df_out['p_value'][ actind ]
            
                for siglvl in siglvls:
                    rejected_percent = p_col[ p_col <= siglvl ].count() / p_col.count()
                    #Add result to the list
                    row_list.append(rejected_percent)
                
                row_dict = dict( zip(column_names,row_list) )
                row_df = pd.DataFrame(row_dict, index = [sim_mode + '-' + str(int(100 * e_sim )) + '-' + str(N) + '-' + str(L)])
                df_results = df_results.append(row_df)

#Export to Excel
df_results.to_excel(fileresults)
